package com.atguigu.utils;


import java.util.Random;

public class ArrayGenerator {

    private ArrayGenerator() {
    }

    public static Integer[] generateOrderArray(int n) {
        Integer[] arr = new Integer[n];
        for (int i = 0; i < n; i++) {
            arr[i] = i;
        }
        return arr;
    }

    /**
     * 生成随机数组
     *
     * @param n     数组的大小
     * @param bound 随机数组中最大值的大小票
     * @return 返回生成的数组
     */
    public static Integer[] generateRandomArray(int n, int bound) {
        Integer[] arr = new Integer[n];
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            arr[i] = random.nextInt(bound);
        }
        return arr;
    }

    public static Integer[] generateSpecialArray(int n) {
        Integer[] arr = new Integer[n];
        generateSpecialArray(arr, 0, arr.length - 1, 0);
        return arr;
    }

    private static void generateSpecialArray(Integer[] arr, int l, int r, int value) {
        if (l > r) {
            return;
        }
        int mid = l + (r - l) / 2;
        arr[mid] = value;
        swap(arr, l, mid);
        generateSpecialArray(arr, l + 1, r, value + 1);
        swap(arr, l, mid);
    }

    private static void swap(Integer[] arr, int l, int r) {
        Integer temp = arr[l];
        arr[l] = arr[r];
        arr[r] = temp;
    }

    public static String[] generateRandomStringArray(int n, int w) {
        String[] arr = new String[n];
        Random rnd = new Random();
        //33-126之间为可打印的字符
        for (int i = 0; i < n; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < w; j++) {
                sb.append((char) (rnd.nextInt(94) + 33));
            }
            arr[i] = sb.toString();
        }
        return arr;
    }
}
